package com.example.new8.presenter;

import android.content.Context;


import com.example.new8.contract.DayImageContact;
import com.example.new8.entity.DayImage;
import com.example.new8.service.DayImageService;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class DayImagePresenter implements DayImageContact.DayImagePresenter {
    private DayImageContact.DayImageView mMainView ;
    private Context mContext ;
    public DayImagePresenter(DayImageContact.DayImageView mMainView , Context mContext){
        this.mMainView = mMainView ;
        this.mContext = mContext ;
    }

    @Override
    public void getData() {
        String url = "https://api.oioweb.cn/api/" ;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        DayImageService service = retrofit.create(DayImageService.class);
        service.getData()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<DayImage>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull DayImage dayImage) {
                        if (mMainView != null){
                            mMainView.showData(dayImage);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
